const CopyWebpackPlugin = require('copy-webpack-plugin')
const path = require('path')

module.exports = {
  // 构件生产模式时是否生成source map（仅在process.env.NODE_ENV === 'production' 时该选项生效）
  productionSourceMap: false,

  // 开启eslint格式化代码
  lintOnSave: true,

  configureWebpack: config => {
    // if (config.mode === 'development') {
    //   config.plugins.push(
    //     new CopyWebpackPlugin([
    //       {
    //         from: 'src/static/vant',
    //         to: 'static/vant'
    //       }
    //     ])
    //   )
    // }
    var usingComponents = {
      'van-tabbar': '/static/vant/tabbar/index',
      'van-tabbar-item': '/static/vant/tabbar-item/index',
      'van-button': '/static/vant/button/index',
      'van-icon': '/static/vant/icon/index',
      'van-field': '/static/vant/field/index',
      'van-tag': '/static/vant/tag/index',
      'van-cell': '/static/vant/cell/index',
      'van-cell-group': '/static/vant/cell-group/index',
      'van-panel': '/static/vant/panel/index',
      'van-rate': '/static/vant/rate/index',
      'van-tabs': '/static/vant/tabs/index',
      'van-tab': '/static/vant/tab/index',
      'van-dialog': '/static/vant/dialog/index',
      'van-datetime-picker': '/static/vant/datetime-picker/index',
      'van-popup': '/static/vant/popup/index',
      'van-area': '/static/vant/area/index',
      'van-picker': '/static/vant/picker/index'
    }
    var copys = [
      {
        from: 'src/static/vant/wxs',
        to: 'static/vant/wxs'
      },
      {
        from: 'src/static/vant/common',
        to: 'static/vant/common'
      },
      {
        from: 'src/static/vant/mixins',
        to: 'static/vant/mixins'
      },
      {
        from: 'src/static/vant/info',
        to: 'static/vant/info'
      },
      {
        from: 'src/static/vant/loading',
        to: 'static/vant/loading'
      },
      {
        from: 'src/static/vant/overlay',
        to: 'static/vant/overlay'
      },
      {
        from: 'src/static/vant/transition',
        to: 'static/vant/transition'
      },
      {
        from: 'src/static/vant/picker-column',
        to: 'static/vant/picker-column'
      }
    ]
    for (var key in usingComponents) {
      var componentName = key.replace('van-', '')
      copys.push({
        from: 'src/static/vant/' + componentName,
        to: 'static/vant/' + componentName
      })
    }

    copys.push({
      from: 'src/static/ec-canvas',
      to: 'static/ec-canvas'
    })
    config.plugins.push(new CopyWebpackPlugin(copys))
  },
  chainWebpack: chainConfig => {
    // 你可以在这里通过 https://github.com/neutrinojs/webpack-chain 来精细的修改webpack配置
    // console.log('chainWebpack执行了', chainConfig.toString())
    chainConfig.module
      .rule('stylus')
      .use('stylus')
      .loader('stylus-loader')
      .options({
        import: [
          path.join(
            __dirname,
            './src/static/css/var.default.styl'
          ),
          path.join(
            __dirname,
            './src/static/css/var.' + chainConfig.store.get('mode') + '.styl'
          ) // 全局变量文件 文件后缀名为.styl
        ]
      })
  },
  // 原生小程序组件存放目录，默认为src/native
  // 如果你有多个平台的原生组件，你应当在此目录下再新建几个子文件夹，我们约定，子文件夹名和平台的名字一致:
  // 微信小程序组件则命名为 'wechat'，支付宝为'alipay', 百度为 'swan'
  // 如果只有一个平台，则无需再新建子文件夹
  nativeDir: '/src/native',

  css: {
    loaderOptions: {
      css: {
        // https://github.com/webpack-contrib/css-loader#options
      },
      less: {
        // https://github.com/webpack-contrib/less-loader
      },
      sass: {
        // https://github.com/webpack-contrib/sass-loader
      },
      stylus: {
        // https://github.com/shama/stylus-loader
      },
      // https://github.com/megalojs/megalo-px2rpx-loader
      px2rpx: {
        rpxUnit: 0.5
      }
    }
  }
}
